package com.hupu.jrs.util

import android.content.Context
import android.content.res.AssetManager
import android.os.Environment

import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

/**
 * Created by sll on 2015/3/7.
 */
class FileUtil {

    fun copy(oldFile: File, newFile: File): Boolean {
        if (!oldFile.exists()) {
            return false
        }
        var inputStream: FileInputStream? = null
        var outputStream: FileOutputStream? = null
        try {
            inputStream = FileInputStream(oldFile)
            outputStream = FileOutputStream(newFile)
            val buffer = ByteArray(4096)
            while (inputStream.read(buffer) != -1) {
                outputStream.write(buffer)
            }
            return true
        } catch (e: Exception) {
            e.printStackTrace()
            return false
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close()
                }
                if (outputStream != null) {
                    outputStream.close()
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }

        }
    }

    companion object {

        fun hasSDCard(): Boolean {
            var mHasSDcard = false
            if (Environment.MEDIA_MOUNTED.endsWith(Environment.getExternalStorageState())) {
                mHasSDcard = true
            } else {
                mHasSDcard = false
            }

            return mHasSDcard
        }

        val sdcardPath: String
            get() = if (hasSDCard()) Environment.getExternalStorageDirectory().absolutePath else "/sdcard/"

        fun chmod(permission: String, path: String) {
            try {
                val command = "chmod $permission $path"
                val runtime = Runtime.getRuntime()
                runtime.exec(command)
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

        fun stringFromAssetsFile(context: Context, fileName: String): String? {
            val manager = context.assets
            val file: InputStream
            try {
                file = manager.open(fileName)
                val data = ByteArray(file.available())
                file.read(data)
                file.close()
                return String(data)
            } catch (e: IOException) {
                e.printStackTrace()
            }

            return null
        }

        /**
         * 复制assets文件到指定目录
         *
         * @param fileName 文件名
         * @param filePath 目录
         */
        fun copyAssets(context: Context, fileName: String, filePath: String) {
            val inputStream: InputStream
            try {
                inputStream = context.resources.assets.open(fileName)// assets文件夹下的文件
                val file = File(filePath)
                if (!file.exists()) {
                    file.mkdirs()
                }
                val fileOutputStream = FileOutputStream(filePath + "/" + fileName)// 保存到本地的文件夹下的文件
                val buffer = ByteArray(1024)
                var count = 0
                while ((count = inputStream.read(buffer)) > 0) {
                    fileOutputStream.write(buffer, 0, count)
                }
                fileOutputStream.flush()
                fileOutputStream.close()
                inputStream.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }

        }

        fun exist(url: String): Boolean {
            val file = File(url)
            return file.exists()
        }
    }
}


